package com.atjg.autocat.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.atjg.autocat.R
import com.atjg.autocat.utils.MusicPlayUtils
import com.atjg.autocat.utils.ZXingUtils
import com.atjg.autocat.viewModel.MainViewModel
import com.lxj.xpopup.core.CenterPopupView
import com.zhy.baselibrary.expand.ToastMgr
import kotlinx.coroutines.Job


@SuppressLint("ViewConstructor")
class QRCodeDialog(var mContext: Context, var url:String,var price:String = "") : CenterPopupView(mContext) {
    private val viewmodel by lazy {
        MainViewModel()
    }
    private var job: Job? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_qr_code
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate() {
        super.onCreate()
        MusicPlayUtils.playBgM(mContext,R.raw.music_scan_pay)
        val tvTime = findViewById<TextView>(R.id.tvTime)
        job = viewmodel.testTime(30000, {
            tvTime.text = "${it}秒后关闭弹框"
        }, {
            dismiss()
        })
        val tvPrice = findViewById<TextView>(R.id.tvPrice)
        if (price.isNotEmpty()){
            tvPrice.visibility=View.VISIBLE
            tvPrice.text = "需支付 $price 元"
        }else{
            tvPrice.visibility=View.GONE
        }
        val ivQrCode = findViewById<ImageView>(R.id.ivQrCode)
        val bitmap = ZXingUtils.createQRImage(url, 500, 500, Color.parseColor("#006400"))
        if (bitmap != null) {
            ivQrCode.setImageBitmap(bitmap)
        } else {
            ToastMgr.show("二维码生成失败")
        }
    }

    override fun onDismiss() {
        super.onDismiss()
        job?.cancel()
    }

}